//
//  ZYHttpTool.m
//  JW_BasePorject
//
//  Created by JW on 2018/6/19.
//  Copyright © 2018年 JW. All rights reserved.
//

#import "ZYHttpTool.h"

@implementation ZYHttpTool{
    ZYRequestMethod _requestType;
    NSDictionary *_requestParams;
    NSString *_requestUrl;
    
}

+ (ZYHttpTool *)requestWithType:(ZYRequestMethod)type urlString:(NSString *)urlString params:(NSDictionary *)params success:(ZYResponseSuccess)success fail:(ZYResponseFail)fail{
    
    ZYHttpTool *httpTool = [[ZYHttpTool alloc] initWithType:type url:urlString params:params];
    [httpTool startWithCompletionBlockWithSuccess:^(__kindof ZYBaseRequest *request) {
        
        success([self jsonPrettyStringEncoded:request.responseObject]);
    } failure:^(__kindof ZYBaseRequest *request) {
        fail(request.error);
    }];
    
    
    return httpTool;
}

+ (id)jsonPrettyStringEncoded:(id)data{
    if ([NSJSONSerialization isValidJSONObject:data]) {
        NSError *error = nil;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data options:NSJSONWritingPrettyPrinted error:&error];
        NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        if (!error){
            return json;
        }
    }
    return data;
}

- (id)initWithType:(ZYRequestMethod)type url:(NSString *)url params:(NSDictionary *)params{
    self = [super init];
    if (self) {
        _requestType = type;
        _requestParams = params;
        _requestUrl = url;
        

    }
    return self;
    
}

//响应数据类型
//- (ZYResponseSerializerType)responseSerializerType {
//    return ZYResponseSerializerTypeHTTP;
//}


//请求url
- (NSString *)requestUrl {
    return _requestUrl;
}

//请求方式
- (ZYRequestMethod)requestMethod {
    return _requestType;
}

//请求头
- (NSDictionary *)requestHeaderFieldValueDictionary {
    return @{};
}

//请求体
- (id)requestArgument {
    return _requestParams;
}
@end
